package cn.tedu.ivos.audit.service.impl;

import cn.tedu.ivos.application.pojo.entity.Application;
import cn.tedu.ivos.audit.mapper.AuditMapper;
import cn.tedu.ivos.audit.pojo.entity.Audit;
import cn.tedu.ivos.audit.service.AuditService;
import cn.tedu.ivos.base.enums.AuditStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Slf4j
@Transactional
@Service
public class AuditServiceImpl implements AuditService {
    @Autowired
    AuditMapper auditMapper;

    @Override
    public void insertAudit(Application application) {
        log.debug("为申请单生成对应的审批单业务:application={}",application);
        //1.获取审批人id集合[106,103]
        List<Long> userIdList = application.getAuditUserIdList();
        //2.遍历审批人集合,有几个审批人,就生成几条审批数据
        for(int i = 0;i<userIdList.size();i++){
            //3.设置当前审批单的相关数据
            Audit audit = new Audit();
            audit.setApplicationId(application.getId());//设置申请单id
            audit.setAuditUserId(userIdList.get(i));//设置审批人id
            //设置审批次序 直属领导次序为0 上上级次序为1
            audit.setAuditSort(i);
            //4.设置每条审批单的状态
            if(i==0){
                //0表示为第1条审批数据,直属领导的审批单状态为"待我审核"
                audit.setAuditStatus(AuditStatusEnum.MY_PENDING.getCode());
            }else{
                //1表示为第2条审批数据,上上级领导的审批单状态为"待他人审核"
                audit.setAuditStatus(AuditStatusEnum.PENDING.getCode());
            }
            //5.设置创建时间
            audit.setCreateTime(new Date());
            //6.每循环一次,向数据库插入一条审批数据
            auditMapper.insert(audit);
        }
    }
}
